//
//  BN_BirthdayView.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-4-8.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_BirthdayView.h"

@implementation BN_BirthdayView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // code
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // 完成按钮添加背景
    [self.completeBtn setBackgroundImage:[UIImage imageNamed:@"daohanglan"] forState:UIControlStateNormal];
    [self.completeBtn addTarget:self action:@selector(completeAction) forControlEvents:UIControlEventTouchUpInside];
    
    // dataPicker
    self.datePicker.frame = CGRectMake(0, FULLSCREEN_HEIGHT-162, SCREEN_WIDTH, 162);
    [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
}

- (void)selectDate {
    // datePicker设置时间
    NSDate *selected = [self.datePicker date];
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:3600*8];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *dateString = [formatter stringFromDate:selected];
    self.birthdayTextField.text = dateString;
    
    // 此处记录生日信息
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:self.birthdayTextField.text forKey:USERDEFAULTS_BIRTHDAYINFO];
    [userDefaults synchronize];
}

- (void)dateChanged:(id)sender {
    // datePicker设置时间
    NSDate *selected = [self.datePicker date];
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:3600*8];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *dateString = [formatter stringFromDate:selected];
    self.birthdayTextField.text = dateString;
    
    // 此处记录生日信息
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:self.birthdayTextField.text forKey:USERDEFAULTS_BIRTHDAYINFO];
    [userDefaults synchronize];
}

- (void)callBackEnterApp:(EnterApp)enterApp {
    _enterApp = enterApp;
}

// 程序开始运行
- (void)completeAction {
    
    if (self.birthdayTextField.text.length > 0) {
        _enterApp(YES);
    }else {
        _enterApp(NO);
        [SVProgressHUD showErrorWithStatus:@"请选择宝贝生日"];
    }
}

@end
